<?php
include_once ('class/autoload.php');
header("Content-Type: text/html; charset=UTF-8");
mb_internal_encoding('UTF-8');
$controleur = new controleur ();
$requete = new PreparationREQ ( $controleur->GetConnectBD () );
if ( $_POST ["type"]=='validationrecup') {
	$sth = $controleur->GetConnectBD ()->prepare ( "SELECT * FROM ecole WHERE valid=0" );
	$sth->execute ();
	$rows = array ();
	while ( $r = $sth->fetch () ) {
		$rows [] = array_map ( 'utf8_encode_array', $r );
	}
	echo $Json = json_encode ( $rows );
	
} else {
	if ($_POST ["type"] == "validation") {
		$so = $requete->Validation ( $_POST ["nom"], $_POST ["ville"] );
		if ($so == 1) {
			echo "OK";
		} else {
			echo $_POST ["nom"];
		}
	}
	if ($_POST ["type"] == "demandeAffiliationrecup") {
		$sth = $controleur->GetConnectBD ()->prepare ( "SELECT * FROM ecole WHERE affiliation=0" );
		$sth->execute ();
		$rows = array ();
		while ( $r = $sth->fetch () ) {
			$rows [] = array_map ( 'utf8_encode_array', $r );
		}
		echo $Json = json_encode ( $rows );
	}
	if ($_POST ["type"] == "demandeAffiliation") {
		$sth = $controleur->GetConnectBD ()->prepare ( "UPDATE ecole SET affiliation = 1 WHERE NOMECOLE=:nom AND VILLE=:ville" );
		$sth->execute (array(':nom'=>utf8_decode($_POST ["nom"]), ':ville'=>utf8_decode($_POST ["ville"])));
		if ($sth->rowCount() == 1) {
			echo "OK";
		} else {
			echo $_POST ["nom"];
		}
	}
	if($_POST['type']=="attenteLicencerecup"){
		$sth = $controleur->GetConnectBD ()->prepare ( "SELECT * FROM ecole WHERE Licence is null" );
		$sth->execute ();
		$rows = array ();
		while ( $r = $sth->fetch () ) {
			$rows [] = array_map ( 'utf8_encode_array', $r );
		}
		echo $Json = json_encode ( $rows );
	}
	
	if($_POST['type']=="licenceEleverecup"){
		$sth = $controleur->GetConnectBD ();
		$getid = $sth->prepare ( "SELECT idecole FROM ecole WHERE nomecole=:nom AND ville=:ville ;" );
		$getid->execute (array(':nom'=>utf8_decode($_COOKIE['nom']), ':ville'=>utf8_decode($_COOKIE['ville'])));
		$id= $getid->fetch();
		$id=$id['idecole'];
		$getEleve=$sth->prepare ( "SELECT * FROM eleve WHERE NUMLICENSE is null AND idecole=:ecole" );
		$getEleve->execute(array(':ecole'=>$id));
		$rows = array ();
		while ( $r = $getEleve->fetch () ) {
			$rows [] = array_map ( 'utf8_encode_array', $r );
		}
		echo $Json = json_encode ( $rows );
	}
}
function utf8_encode_array($array) {
	return utf8_encode ( $array );
}
?>